/*
 * 注意：本程序中的“随机”都是伪随机概念，以当前的天为种子。
 */
function random(dayseed, indexseed) {
	var n = dayseed % 11117;
	for (var i = 0; i < 100 + indexseed; i++) {
		n = n * n;
		n = n % 11117;   // 11117 是个质数
	}
	return n;
}

var today = new Date();
var iday = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate();

var weeks = ["日","一","二","三","四","五","六"];
var directions = ["北方","东北方","东方","东南方","南方","西南方","西方","西北方"];
var activities = [
	{name:"跟情郎约会", good:"获得一次预约的感官之旅",bad:"被抓奸的可能性很高"},
	{name:"眉目传情", good:"对方收到信号，心里甜蜜蜜",bad:"被小人挡住视线", weekend: true},
	{name:"摸摸他的手", good:"爱情正能量提升",bad:"他可能没洗手", weekend: true},
	{name:"抽烟", good:"提升女性魅力",bad:"FUCK，文件夹点着了", weekend: true},
	{name:"上班玩游戏", good:"今天白天上线是安全的",bad:"可能导致灾难性后果"},
	{name:"写文档", good:"其实也没啥好事",bad:"你很有可能会陷入泥潭"},
	{name:"翘班跟情郎出去玩", good:"好开心好开心",bad:"还是好开心"},
	{name:"淡妆", good:"原滋原味有味道",bad:"要上台表演脸色惨白"},
	{name:"带卡帕石", good:"有钱有品味求包养",bad:"被坏人盯上了"},
	{name:"骂老板一顿", good:"心情很爽",bad:"去财务结账"},
	{name:"色诱右手边", good:"哦哦也",bad:"大姨妈来了"},
	{name:"周五去约会", good:"赞啦必须去",bad:"嗯，周五还是可以去的"},
	{name:"晚上幽会", good:"晚上是最好的时候",bad:"", weekend: true},
	{name:"听右边调情", good:"办公室奸情好兴奋",bad:"波波插了过来", weekend: true},
	{name:"听歌", good:"精神振奋吧",bad:"要睡着了", weekend: true},
	{name:"亲吻右手边的帅哥", good:"跟他说一起去幽会吧",bad:"你会脸红耳赤被发现", weekend: true},
	{name:"来约会吧", good:"很好玩的哦",bad:"反正很好玩"},
	{name:"面对面打电话", good:"我很想你哟亲爱的",bad:"别透露幽会地址哦"},
	{name:"市场调查", good:"遇到帅哥",bad:"连弱智都知道的事情做毛的市场调查"},
	{name:"文档复审", good:"发现重要问题的几率大大增加",bad:"你什么问题都发现不了，白白浪费时间"},
	{name:"评审文档", good:"一群傻逼没睡醒，忽悠过关",bad:"这帮傻逼是原始部落穿越来的吧"},
	{name:"写邮件", good:"你将有如神助",bad:"自己都不知所云", weekend: true},
	{name:"晚上游戏", good:"晚上是精神最好的时候",bad:"你白天已经筋疲力尽了"},
	{name:"改写文档", good:"你今天对BUG的嗅觉大大提高",bad:"新产生的BUG将比之前的更多"},
	{name:"设计评审", good:"设计评审会议将变成头脑风暴",bad:"人人筋疲力尽，评审就这么过了"},
	{name:"需求评审", good:"",bad:""},
	{name:"上微博", good:"今天发生的事不能错过",bad:"今天的微博充满负能量", weekend: true},
	{name:"上AB站", good:"还需要理由吗？",bad:"满屏的兄贵我会说出来？", weekend: true}
];

var specials = [
	{date:20130528, type:'good', name:'看星际迷航', description:''}
];

var tools = ["Eclipse写程序", "MSOffice写文档", "记事本写程序", "Windows8", "Linux", "MacOS", "IE", "Android设备", "iOS设备"];

var varNames = ["jieguo", "huodong", "pay", "expire", "zhangdan", "every", "free", "i1", "a", "virtual", "ad", "spider", "mima", "pass", "ui"];

var drinks = ["水","茶","红茶","绿茶","咖啡","奶茶","可乐","牛奶","豆奶","果汁","果味汽水","苏打水","运动饮料","酸奶","酒"];

function getTodayString() {
	return "今天是" + today.getFullYear() + "年" + (today.getMonth() + 1) + "月" + today.getDate() + "日 星期" + weeks[today.getDay()];
}

function star(num) {
	var result = "";
	var i = 0;
	while (i < num) {
		result += "★";
		i++;
	}
	while(i < 5) {
		result += "☆";
		i++;
	}
	return result;
} 

// 生成今日运势
function pickTodaysLuck() {
  var _activities = filter(activities);
    
	var numGood = random(iday, 98) % 3 + 2;
	var numBad = random(iday, 87) % 3 + 2;
	var eventArr = pickRandomActivity(_activities, numGood + numBad);
	
	var specialSize = pickSpecials();
	
	for (var i = 0; i < numGood; i++) {
		addToGood(eventArr[i]);
	}
	
	for (var i = 0; i < numBad; i++) {
		addToBad(eventArr[numGood + i]);
	}
}

// 去掉一些不合今日的事件
function filter(activities) {
    var result = [];
    
    // 周末的话，只留下 weekend = true 的事件
    if (isWeekend()) {
        
        for (var i = 0; i < activities.length; i++) {
            if (activities[i].weekend) {
                result.push(activities[i]);
            }
        }
        
        return result;
    }
    
    return activities;
}

function isWeekend() {
    return today.getDay() == 0 || today.getDay() == 6;
}

// 添加预定义事件
function pickSpecials() {
	var specialSize = [0,0];
	
	for (var i = 0; i < specials.length; i++) {
		var special = specials[i];
		
		if (iday == special.date) {
			if (special.type == 'good') {
				specialSize[0]++;
				addToGood({name: special.name, good: special.description});
			} else {
				specialSize[1]++;
				addToBad({name: special.name, bad: special.description});
			}
		}
	}
	
	return specialSize;
}

// 从 activities 中随机挑选 size 个
function pickRandomActivity(activities, size) {
	var picked_events = pickRandom(activities, size);
	
	for (var i = 0; i < picked_events.length; i++) {
		picked_events[i] = parse(picked_events[i]);
	}
	
	return picked_events;
}

// 从数组中随机挑选 size 个
function pickRandom(array, size) {
	var result = [];
	
	for (var i = 0; i < array.length; i++) {
		result.push(array[i]);
	}
	
	for (var j = 0; j < array.length - size; j++) {
		var index = random(iday, j) % result.length;
		result.splice(index, 1);
	}
	
	return result;
}

// 解析占位符并替换成随机内容
function parse(event) {
	var result = {name: event.name, good: event.good, bad: event.bad};  // clone
	
	if (result.name.indexOf('%v') != -1) {
		result.name = result.name.replace('%v', varNames[random(iday, 12) % varNames.length]);
	}
	
	if (result.name.indexOf('%t') != -1) {
		result.name = result.name.replace('%t', tools[random(iday, 11) % tools.length]);
	}
	
	if (result.name.indexOf('%l') != -1) {
		result.name = result.name.replace('%l', (random(iday, 12) % 247 + 30).toString());
	}
	
	return result;
}

// 添加到“宜”
function addToGood(event) {
	$('.good .content ul').append('<li><div class="name">' + event.name + '</div><div class="description">' + event.good + '</div></li>');
}

// 添加到“不宜”
function addToBad(event) {
	$('.bad .content ul').append('<li><div class="name">' + event.name + '</div><div class="description">' + event.bad + '</div></li>');
}

$(function(){
	$('.date').html(getTodayString());
	$('.direction_value').html(directions[random(iday, 2) % directions.length]);
	$('.drink_value').html(pickRandom(drinks,2).join('，'));
	$('.goddes_value').html(star(random(iday, 6) % 5 + 1));
	pickTodaysLuck();
});